package com.emm.yixun.website.controller.pmo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.common.model.ProjectPmo;
import com.emm.yixun.common.model.SaleReturnPmo;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.ProjectPmoDto;
import com.emm.yixun.common.model.enums.SaleReturnEnum;
import com.emm.yixun.common.model.pmo.SalesBackOfYear;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.utils.DateStyle;
import com.emm.yixun.common.utils.DateUtils;
import com.emm.yixun.estate.service.ProjectPmoServiceFacade;
import com.emm.yixun.estate.service.ProjectServiceFacade;
import com.emm.yixun.system.service.BusinessLogServiceFacade;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;
import com.emm.yixun.website.service.pmo.LevelProjectDataService;

/**
 * 补录历年销售数据
 */
@Controller
@RequestMapping(value = "/pmo/salesBackOfYearPmo/")
public class SalesBackOfYearPmoController extends BaseController<Object> {

	@Autowired
	private ProjectServiceFacade projectServiceFacade;
	@Autowired
	private LevelProjectDataService levelProjectDataService;
	@Autowired
	private ProjectPmoServiceFacade projectPmoServiceFacade;
	@Autowired
	private BusinessLogServiceFacade businessLogServiceFacade;

	@RequestMapping(value = "index")
	public ModelAndView index() {
		return new ModelAndView("/salesBack/index");
	}

	@RequestMapping(value = "findSalesPlan")
	public @ResponseBody Map<String, Object> findSalesPlan(Long projectId, String year) {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			// 销售计划
			ResponseList<SaleReturnPmo> responseSaleList = projectServiceFacade.querySaleReturnListPmo(projectId, null,
					SaleReturnEnum.SALE, year);
			// 回款计划
			ResponseList<SaleReturnPmo> responseReturnList = projectServiceFacade.querySaleReturnListPmo(projectId, null,
					SaleReturnEnum.RETURN, year);
			// 计划销售额
			returnMap.put("srpSaleMap", responseSaleList.getEntity());
			// 计划回款额
			returnMap.put("srpReturnMap", responseReturnList.getEntity());
		}catch(Exception ex) {
			logger.error("计划销售,回款额错误:", ex);
		}
		return returnMap;
	}

	/**
	 * 补录历史年份销售数据
	 * 
	 * @param projectId
	 * @return
	 */
	@RequestMapping(value = "sendLevelProjectData")
	public @ResponseBody AjaxRes sendLevelProjectData(Long projectId, SalesBackOfYear salesBackOfYear) {
		AjaxRes ar = getAjaxRes();
		User user = ShiroUtil.getCurrentUser();
		try {
			ProjectPmoDto pmo = new ProjectPmoDto();
			pmo.setProjectId(projectId);
			ResponseList<ProjectPmo> responseList = projectPmoServiceFacade.findByDto(pmo);
			if (responseList != null && !responseList.getEntity().isEmpty()) {
				String month = DateUtils.DateToString(new Date(), DateStyle.YYYYMM.getValue());
				List<SalesBackOfYear> list = new ArrayList<SalesBackOfYear>();
				list.add(salesBackOfYear);
				levelProjectDataService.sendLevelDyData(responseList.getEntity().get(0).getPmoProjectCode(), month, list);
				ar.setRes(Const.SUCCEED);
			}
		}catch(Exception ex) {
			logger.error("补录历史年份销售数据", ex);
			//新增业务操作异常记录
			businessLogServiceFacade.saveExceptionLog(user, "补录历史年份销售数据", JSONObject.toJSONString(salesBackOfYear), JSONObject.toJSONString(ex));
		}
		
		return ar;
	}
}
